﻿using System;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Collections.Generic;
using Model.Query_Model;
using Model.Filters;
using Datasource;

namespace Presenters.Managers
{
    public class RoadManager
    {
        private IList<RoadOutput> roadList = new List<RoadOutput>();

        private IList<RoadOutput> BuildCollection(DbDataReader reader)
        {
            roadList.Clear();
            while (reader.Read())
            {
                roadList.Add(new RoadOutput
                {
                    idRoad=reader.GetInt32(0),
                    toCity=reader.GetString(1),
                    fromCity=reader.GetString(2),
                    toCountry=reader.GetString(3),
                    fromCountry=reader.GetString(4),
                    client= (ClientOutput)ClientManager.SearchClient(new ClientCriteria{ idRoad=reader.GetInt32(0)}).Take(1).FirstOrDefault<ClientOutput>(),
                    valuation=(ValuationOutput)ValuationManager.ValuationSearch(new ValuationCriteria{idRoad=reader.GetInt32(0)}).Take(1).FirstOrDefault<ValuationOutput>()
                }
                );
            }
            return roadList;
        }

        public IList<RoadOutput> RoadSearch(RoadCriteria criteria)
        {
            DbConnection connection = DbUtil.GetActivityConnection();
            DbCommand command = new SqlCommand();
            DbUtil.SetConnection(connection, command);
            DbUtil.SetQuery(command, new DbQuery { sql = "exec [dbo].[SearchRoad] fromCity, NULL, NULL, NULL, NULL, NULL, NULL" });
            /*DbUtil.SetParametr(command, "fromCity", criteria.fromCity);
            DbUtil.SetParametr(command, "@toCity", criteria.toCity);
            DbUtil.SetParametr(command, "@fromCountry", criteria.fromCountry);
            DbUtil.SetParametr(command, "@toCountry", criteria.toCountry);
            DbUtil.SetParametr(command, "@nameCorporation", criteria.nameCorporation);
            DbUtil.SetParametr(command, "@priceStart", criteria.priceStart);
            DbUtil.SetParametr(command, "@priceStop", criteria.priceStop);*/
            return BuildCollection(command.ExecuteReader());
        }

        public void RoadDelete(RoadOutput road)
        {
            DbConnection connection = DbUtil.GetActivityConnection();
            DbCommand command = new SqlCommand();
            DbUtil.SetConnection(connection, command);
            DbUtil.SetQuery(command, new DbQuery { sql = "exec [dbo].[RoadDelete] @idRoad" });
            DbUtil.SetParametr(command, "@idRoad", road.idRoad);
            DbUtil.ExecuteNonQuery(command);
            DbUtil.CloseActivityConnection(connection);
        }

        public void RoadUpdate(RoadOutput road)
        {
            DbConnection connection = DbUtil.GetActivityConnection();
            DbCommand command = new SqlCommand();
            DbUtil.SetConnection(connection, command);
            DbUtil.SetQuery(command, new DbQuery { sql = "exec [dbo].[RoadUpdate] @idRoad, @fromCity, @fromCountry, @toCity, @toCountry, @idType" });
            DbUtil.SetParametr(command, "@fromCity", road.fromCity);
            DbUtil.SetParametr(command, "@fromCountry", road.fromCountry);
            DbUtil.SetParametr(command, "@toCity", road.toCity);
            DbUtil.SetParametr(command, "@toCountry", road.toCountry);
            DbUtil.SetParametr(command, "@idType", 1);
            DbUtil.SetParametr(command, "@idRoad", road.idRoad);
            DbUtil.ExecuteNonQuery(command);
            DbUtil.CloseActivityConnection(connection);
        }

        public void Dispose()
        {
            this.Dispose();
        }
    }
}
